﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class EnemySpawner : MonoBehaviour
{
    public Text time_text;
    public GameObject ahead_button;
    private int start_time;
    private float timer;
    public EnemyWave[] waves = new EnemyWave[4];
    public static Coroutine spawner;
    public Transform start;
    private Transform start_pos;
    public static int enemy_sum;

    private void Awake()
    {
        start_pos = start;
    }
    private void Start()
    {
        enemy_sum = 0;
        start_time = 600;
        spawner = StartCoroutine("SpawnEnemy");
    }

    public void AheadTime()
    {
        start_time = 0;
        Destroy(ahead_button);
    }
    
    IEnumerator SpawnEnemy()
    {
        while (start_time > 0)
        {
            if (start_time % 60 == 0)
            {
                time_text.text = "敌人出现时间：  " + (start_time / 60).ToString();
            }
            start_time--;
            yield return 0; //跳过这一帧
        }
        time_text.text = "敌人已出现";
        Destroy(ahead_button);
        for (int i=0;i<4;i++)
        {
            for(int j=0;j< waves[i].spawn_sum; j++)
            {
                enemy_sum++;
                GameObject.Instantiate(waves[i].enemy_type, start_pos.position,Quaternion.identity);
                yield return new WaitForSeconds(waves[i].spawn_rate);
            }
            yield return new WaitForSeconds(waves[i].wave_rate);
        }
        while (enemy_sum > 0)  //还有敌人存在时等待一帧再检查
        {
            yield return 0; //跳过这一帧
        }
        GameManager.instance.Win(); //判断胜利
    }
}
